package com.demo.controller.admin;

import com.demo.common.model.Nav;
import com.demo.controller.BaseController;
/**
 * 
 * @author Emode
 * 首页导航管理
 */
public class NavAdminController extends BaseController<Nav>{
	
	public void index(){
		renderJson(Nav.me.paginate( getParaToInt("page",1), 10, getQueryParam()));
	}
	
	public void toEdit(){
		String id = getPara("id");
		setAttr("nav", Nav.me.findById(id));
		render("/admin/system/system-nav-edit.html");
		
	}
	//操作保存或更新
	public void saveOrUpdate(){
		Nav nav = new Nav();
		Boolean bool = false;
		String id = getPara("id","");
		String name = getPara("name");
		String url = getPara("url");
		String order = getPara("order");
		String status = getPara("status","0");
		nav.set("name", name)
			.set("url", url)
			.set("order", order)
			.set("status", status);
		if(id!=null&&!id.equals("")){
			bool = nav.set("id", id).update();
		}else{
			bool = nav.save();
		}
		if(bool){
			success("保存成功");
		}else{
			error("保存失败");
		}
	}
	
	// 批量删除
	public void batchDel() {
		String[] ids = getPara("ids").split(",");
		Boolean bool = false;
		if (ids != null && ids.length > 0) {
			for (int i = 0; i < ids.length; i++) {
				bool = Nav.me.deleteById(ids[i]);
			}

		}
		if (bool) {
			success("操作成功");
		} else {
			error("操作失败");
		}
	}
	
	//操作
	//是否禁用
	public void operate() {
		String type = getPara("type");
		Nav nav =  Nav.me.findById(getPara("id"));
		if ("yes".equals(type)) {  //确认发布时
			nav.set("status", 1);
		} else {
			nav.set("status", 0);
		}
		
		Boolean bool = nav.update();
		if (bool) {
			success("操作成功");
		} else {
			error("操作失败");
		}
	}
		

}
